package cc.openkit.admin.controller.common;

import cc.openkit.admin.model.PaySettingWeixin;
import cc.openkit.admin.model.util.PayUtil;
import cc.openkit.admin.service.pay.paySetting.PaySettingService;
import cc.openkit.admin.service.pay.paySettingAlipay.PaySettingAlipayService;
import cc.openkit.admin.service.pay.paySettingWeixin.PaySettingWeixinService;
import cc.openkit.admin.util.SpringContextUtil;
import cc.openkit.kitPay.weichart.model.WeichartModel;
import cc.openkit.kitPay.weichart.service.WeixinPayService;
import cc.openkit.kitPay.weichart.service.impl.WeixinPayServiceImpl;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付的公共接口
 */
public class KitPay {

    private Logger log = Logger.getLogger(ApiCommon.class);

    @Resource
    private static PaySettingService paySettingService;
    @Resource
    private static PaySettingAlipayService paySettingAlipayService;
    @Resource
    private static PaySettingWeixinService paySettingWeixinService;



    /**
     * 通用接口
     * @param request
     * @param response
     * @param payUtil
     * @return
     */
    public static Map<String, Object> kitAppPayUnifyAPI(HttpServletRequest request, HttpServletResponse response, PayUtil payUtil) throws Exception{

        // Service 层调用容器
//        PaySettingWeixinService paySettingWeixinService= (PaySettingWeixinService) SpringContextUtil.getBean("paySettingWeixinService");

        Map<String, Object> map = new HashMap<String, Object>();
        if("WEIXINPAY".equals(payUtil.getType())){
            // 微信支付
            // 数据库查找微信支付配置参数，封装数据
            WeichartModel weichartModel = new WeichartModel();

            PaySettingWeixin wx = paySettingWeixinService.queryById(1);

            weichartModel.setAppId(wx.getPswAppId());
            weichartModel.setAppSecret(wx.getPswAppSecret());
            weichartModel.setAppKey(wx.getPswAppKey());
            weichartModel.setMchId(wx.getPswMchid());
            weichartModel.setBody(wx.getPswBody());
            weichartModel.setPartnerKey(wx.getPswPartnerKey());
            weichartModel.setPartnerId(wx.getPswPartnerId());
            weichartModel.setGrantType(wx.getPswGrantType());
            weichartModel.setGateUrl(wx.getPswGateUrl());
            weichartModel.setNotifyUrl(wx.getPswNotifyUrl());
            weichartModel.setSpbillCreateIp(wx.getPswSpbillCreateip());

            // 调用方法
            WeixinPayService weixinPayService = new WeixinPayServiceImpl();
            map = weixinPayService.getOrder(request, response, weichartModel,payUtil.getMoney(), payUtil.getOrderNumber(), payUtil.getNotifyUrl());
        }

        if("ALIPAY".equals(payUtil.getType())){
            // 支付宝支付
        }
        return map;
    }

}
